﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Edit.Master" Inherits="System.Web.Mvc.ViewPage<IntellectShop.Web2.Hardware.ViewModels.AdminViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Управление пользователями
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
	<script type="text/javascript">
		$(document).ready(function () {
			// verify the delete intent
			$(".delete").click(function (e) {
				if (!confirm("Вы уверены в том, что хотите удалить?")) {
					return false;
				}
				return true;
			});
		});
	</script>
	<div class="page round border">
		<div id="users" class="content-area">
			<h1 class="headline">Пользователи</h1>
			<% if (TempData["UserError"] != null)%>
			<% { %>
			<div class="info">
				<%= TempData["UserError"]%>
			</div>
			<%}%>
			<div>
				<% if (Model.Users.Count > 0) %>
				<% { %>
				<table class="users">
					<tr>
						<th>
							Имя
						</th>
						<th>
							Email
						</th>
						<th>
							Онлайн?
						</th>
						<th>
							Одобрен
						</th>
						<th>
							Заблокирован
						</th>
						<th>
							Пароль
						</th>
						<th>
							Назначенные роли
						</th>
						<th>
						</th>
					</tr>
					<% foreach (MembershipUser user in Model.Users) %>
					<% { %>
					<tr>
						<td>
							<%= Html.Encode(user.UserName) %>
						</td>
						<td>
							<%= Html.Encode(user.Email) %>
						</td>
						<% if (user.IsOnline) %>
						<%{ %>
						<td class="isOnline">
							Да
						</td>
						<% }%>
						<%else%>
						<%{ %>
						<td>
							Offline -
							<%
								var offlineSince = (DateTime.Now - user.LastActivityDate);
								Response.Write(Math.Floor(offlineSince.TotalDays) + " дней.");
							%>
						</td>
						<% } %>
						<td>
							<a href='<%= Url.Action("ToggleApproved", "Account", new {userName=user.UserName}) %>'>
								<%=Html.Encode(user.IsApproved) %>
							</a>
						</td>
						<td>
							<%if (user.IsLockedOut)%>
							<% {%>
							<%= Html.ActionLink("True", "UnlockUser", "Account", new {userName = user.UserName}, null) %>
							<%}%>
							<%else%>
							<%{%>
							<%= Html.Encode(user.IsLockedOut) %>
							<%}%>
						</td>
						<td>
							<a href='<%= Url.Action("ResetPassword", "Account", new {userName=user.UserName}) %>'>Сбросить</a>
						</td>
						<td>
							<% string rolesForUser = string.Join(",", Roles.GetRolesForUser(user.UserName)); %>
							<% foreach (string role in Model.Roles)%>
							<%{%>
								<span class='<%= rolesForUser.Contains(role) ? "member" : "nonmember" %>'>
									<a href='<%= Url.Action("ToggleRole", "Account", new { role = role, userName = user.UserName}) %>'><%= Html.Encode(role) %></a>
								</span>
							<%}%>
						</td>
						<td>
							<a class="delete" href='<%= Url.Action("DeleteUser", "Account", new {userName=user.UserName}) %>'>удалить</a>
						</td>
					</tr>
					<% } %>
				</table>
				<% }%>
				<%else%>
				<%{ %>
				<p>Нет зарегистрированых пользователей.</p>
				<% } %>
			</div>
		</div>

		<div class="spacer">&nbsp;</div>

		<div id="roles" class="content-area">
			<h1 class="headline">Роли</h1>

			<% if (TempData["RoleError"] != null)%>
			<%{%>
			<div class="info">
				<%= TempData["RoleError"]%>
			</div>
			<%}%>

			<div>
				<% if (Model.Roles.Length > 0)%>
				<%{ %>
				<ul>
					<% foreach (string role in Model.Roles)%>
					<%{ %>
					<li>
						<%= Html.Encode(role) %>
						<% if (role != IntellectShop.Common.Globals.AdministratorRoleName)%>
						<%{ %>
						[ <a class="delete" href='<%= Url.Action("DeleteRole", "Account", new {role=role}) %>'>удалить</a> ]
						<% } %>
					</li>
					<% } %>
				</ul>
				<% }%>
				<%else%>
				<%{ %>
				Нет ролей.
				<% } %>

				<div class="spacer">&nbsp;</div>

				<div class="commands">
					<%	using (Html.BeginForm("AddRole", "Account", FormMethod.Post))%>
					<%{%>
							<span class="formlabel">
								Новая роль:
							</span>
							<span class="forminput">
								<input id="role" name="newRole" type="text" />
								<input id="addRole" type="submit" value="Добавить" style="width: 70px;"/>
							</span>
					<%}%>
				</div>
			</div>
		</div>
	</div>
</asp:Content>
